<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
             label-position="left">

      <div class="title-container">
        <h3 class="title">EasyJava云平台</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入登录账号"
          name="username"
          type="text"
          maxlength="20"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入登录密码"
          name="password"
          tabindex="2"
          maxlength="20"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>

      <!--  验证码    -->
      <el-form-item prop="imageCode">
        <span class="svg-container">
          <svg-icon icon-class="imageCode"/>
        </span>
        <el-input
          ref="imageCode"
          v-model="loginForm.imageCode"
          placeholder="请输入图片验证码"
          name="imageCode"
          type="text"
          tabindex="3"
          maxlength="4"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;background-color: #ee4d2d;border-color: #ee4d2d;" @click.native.prevent="handleLogin">登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">账号: admin</span>
        <span> 密码: 123456</span>
      </div>

    </el-form>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2020-{{ currentYear }}
<!--        <a style="text-decoration: underline" @click="linkMe = true">bobo</a>-->
        All Rights Reserved.</span>
    </div>

    <el-dialog
      title="联系作者"
      :visible.sync="linkMe"
      width="30%">
      <div style="text-align: center;">
        <span style="color:#ff4949;">
           微信扫码，联系作者一对一交流
        </span>
      </div>
      <div style="text-align: center;">
        <img src="../../assets/images/wjb_weixin.jpg" style="width: 230px;height: 230px;">
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getImageCode, login
} from "@/api/user";

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('请输入登录账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('请输入登录密码'))
      } else {
        callback()
      }
    }
    const validateImageCode = (rule, value, callback) => {
      if (!value || value.length < 1) {
        callback(new Error('请输入图片验证码'))
      } else {
        callback()
      }
    }
    return {
      currentYear: '',
      loginForm: {
        username: '',
        password: '',
        uuid: '',
        imageCode: '',
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}],
        imageCode: [{required: true, trigger: 'blur', validator: validateImageCode}],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      linkMe: false,
      codeUrl: '',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  created() {
    const timeOne = new Date()
    this.currentYear = timeOne.getFullYear();
    this.getCode();
  },

  methods: {
    //获取随机UUID
    createUuid() {
      let s = [];
      let hexDigits = "0123456789abcdefghi";
      for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";
      let uuid = s.join("");
      return uuid;
    },

    //获取图片验证码
    getCode() {
      let imageCodeKey = localStorage.getItem("easyjavatemplateImageCodeKey");
      if (!imageCodeKey || imageCodeKey === '' || imageCodeKey == null) {
        //第一次进入页面
        imageCodeKey = this.createUuid();
        localStorage.setItem("easyjavatemplateImageCodeKey", imageCodeKey);
      }
      this.loginForm.uuid = imageCodeKey;
      getImageCode(imageCodeKey).then(result => {
        this.codeUrl = result.data;
      }).catch(error => {
        this.$message.error(error.message);
        return;
      });
    },

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$message({
              type: 'success',
              message: '登录成功'
            });
            this.$router.push({path: this.redirect || '/dashboard'})
            this.loading = false
          }).catch((e) => {
            console.log("11111111111111111111");
            console.log('e---->' + JSON.stringify(e));
            this.getCode();
            this.loginForm.imageCode = '';
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 70%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  /* 设置背景图片 */
  background-image: url('../../assets/images/bg_login_5.png');
  /* 确保背景图片覆盖整个屏幕 */
  background-size: cover;
  background-position: center;
  /* 防止内容溢出 */
  overflow: hidden;
  /* 让div占据整个视口 */
  width: 100vw;
  height: 100vh;
  /* 可选：让背景图片固定不动 */
  background-attachment: fixed;
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  //background: url('../../assets/images/bg1.jpg') no-repeat;
  overflow: hidden;
  background-size: cover;
  position: absolute;

  .login-form {
    position: absolute; /* 或 fixed，取决于你是否希望表单随页面滚动而移动 */
    top: 25%; /* 距离顶部距离 */
    left: 60%; /* 距离左侧宽度 */
    //下面是新修改的样式
    -webkit-border-radius: 28px;
    border-radius: 28px;
    -moz-border-radius: 28px;
    background-clip: padding-box;
    width: 550px;
    padding: 35px 35px 15px 35px;
    background: $bg;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    text-align: center;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 36px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .el-login-footer {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    color: #ffffff;
    font-family: Arial;
    font-size: 15px;
    letter-spacing: 1px;
  }
}

.login-code {
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 38px;
  margin-top: 7px;
  border-radius: 4px;
  margin-right: 4px;
}
</style>
